package hierarchy

import (
	"gitee.com/g-qs/gin-vue-admin/server/global"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/request"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/response"
	"gitee.com/g-qs/gin-vue-admin/server/model/hierarchy"
	hierarchyReq "gitee.com/g-qs/gin-vue-admin/server/model/hierarchy/request"
	"gitee.com/g-qs/gin-vue-admin/server/service"
	"gitee.com/g-qs/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type LevelOneApi struct {
}

var levelOneService = service.ServiceGroupApp.HierarchyServiceGroup.LevelOneService

// CreateLevelOne 创建LevelOne
// @Tags LevelOne
// @Summary 创建LevelOne
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.LevelOne true "创建LevelOne"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /levelOne/createLevelOne [post]
func (levelOneApi *LevelOneApi) CreateLevelOne(c *gin.Context) {
	var levelOne hierarchy.LevelOne
	err := c.ShouldBindJSON(&levelOne)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Key":   {utils.NotEmpty()},
		"Value": {utils.NotEmpty()},
	}
	if err := utils.Verify(levelOne, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelOneService.CreateLevelOne(&levelOne); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteLevelOne 删除LevelOne
// @Tags LevelOne
// @Summary 删除LevelOne
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.LevelOne true "删除LevelOne"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /levelOne/deleteLevelOne [delete]
func (levelOneApi *LevelOneApi) DeleteLevelOne(c *gin.Context) {
	var levelOne hierarchy.LevelOne
	err := c.ShouldBindJSON(&levelOne)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelOneService.DeleteLevelOne(levelOne); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteLevelOneByIds 批量删除LevelOne
// @Tags LevelOne
// @Summary 批量删除LevelOne
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除LevelOne"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /levelOne/deleteLevelOneByIds [delete]
func (levelOneApi *LevelOneApi) DeleteLevelOneByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelOneService.DeleteLevelOneByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateLevelOne 更新LevelOne
// @Tags LevelOne
// @Summary 更新LevelOne
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.LevelOne true "更新LevelOne"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /levelOne/updateLevelOne [put]
func (levelOneApi *LevelOneApi) UpdateLevelOne(c *gin.Context) {
	var levelOne hierarchy.LevelOne
	err := c.ShouldBindJSON(&levelOne)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Key":   {utils.NotEmpty()},
		"Value": {utils.NotEmpty()},
	}
	if err := utils.Verify(levelOne, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelOneService.UpdateLevelOne(levelOne); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindLevelOne 用id查询LevelOne
// @Tags LevelOne
// @Summary 用id查询LevelOne
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchy.LevelOne true "用id查询LevelOne"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /levelOne/findLevelOne [get]
func (levelOneApi *LevelOneApi) FindLevelOne(c *gin.Context) {
	var levelOne hierarchy.LevelOne
	err := c.ShouldBindQuery(&levelOne)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if relevelOne, err := levelOneService.GetLevelOne(levelOne.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"relevelOne": relevelOne}, c)
	}
}

// GetLevelOneList 分页获取LevelOne列表
// @Tags LevelOne
// @Summary 分页获取LevelOne列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.LevelOneSearch true "分页获取LevelOne列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /levelOne/getLevelOneList [get]
func (levelOneApi *LevelOneApi) GetLevelOneList(c *gin.Context) {
	var pageInfo hierarchyReq.LevelOneSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := levelOneService.GetLevelOneInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetLearnLevelOneList 分页获取学习文章一级列表
// @Tags LevelOne
// @Summary 分页获取学习文章一级列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.LevelOneSearch true "分页获取学习文章一级列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /levelOne/getLearnLevelOneList [get]
func (levelOneApi *LevelOneApi) GetLearnLevelOneList(c *gin.Context) {
	var pageInfo hierarchyReq.LevelOneSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := levelOneService.GetLearnLevelOneInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
